// 用户任务节点
import {GraphModel, h, LogicFlow, RectNode, RectNodeModel} from '@logicflow/core'
import {userTaskIcon} from '../icon'
import {genBpmnId} from "@logicflow/extension/src/bpmn-elements/utils";
import {FlowNodeTypeEnum} from "@/enums/FlowNodeTypeEnum"
import {sourceRulesNotSameOut} from "./rules/NodeValidateRules"
// 自定义业务节点外观
class MyRectView extends RectNode {
    getShape() {
        // 获取XxxNodeModel中定义的形状属性
        const {model} = this.props
        const {x, y, width, height, radius, properties} = model
        // 获取XxxNodeModel中定义的样式属性
        const style = model.getNodeStyle()

        return h('g', {}, [
            h('rect', {
                ...style,
                x: x - width / 2,
                y: y - height / 2,
                rx: 5,
                ry: 5,
                width,
                height,
                opacity: 0.95,
            }),
            h(
                'svg',
                {
                    x: x - width / 2 + 5,
                    y: y - height / 2 + 5,
                    width: 25,
                    height: 25,
                    viewBox: '0 0 1274 1024',
                },
                h('path', {
                    fill: style.stroke,
                    d: userTaskIcon,
                }),
            ),

        ])
    }
}

class MyRectNodeModel extends RectNodeModel {
    constructor(data: NodeConfig, graphModel: GraphModel) {
        if (!data.id) {
            data.id = `user_task_${genBpmnId()}`
        }
        super(data, graphModel)
    }

    // 自定义节点形状（圆角矩形）
    initNodeData(data: LogicFlow.NodeConfig) {
        super.initNodeData(data);
        this.width = 100
        this.height = 80
        this.radius = 10
    }

    //自定义业务节点样式
    getNodeStyle() {
        const style = super.getNodeStyle()
        //style.stroke = '#47ed2c'
        return style
    }

    // 获取当前节点作为边的起始节点规则
    getConnectedSourceRules(): Model.ConnectRule[] {
        const rules = super.getConnectedSourceRules();
        rules.push(sourceRulesNotSameOut);
        return rules;
    }
}

// 注册组件
export function register(lf: LogicFlow) {
    lf.register({
        type: FlowNodeTypeEnum.USER_TASK,
        view: MyRectView,
        model: MyRectNodeModel,
    })
}

